$(function() {

	var baseUrl = "https://caga.cyberagent.co.jp";
	
	//日スケジュール取得
	var getDaySchedule = function(uid, date) {
		//date:yyyy-MM-dd
		var daySchedule = "https://caga.cyberagent.co.jp/cgi-bin/cbgrn/grn.cgi/schedule/personal_day?"
		var dateQuery = "bdate=" + date;
		var uidQuery = "&uid=" + uid;

		$.get(daySchedule + dateQuery + uidQuery, function(_data) {
			var $bodyHtml = $(_data);
			var $schedules = $("div.showtime div.critical");
			
		});
	};
	
	//スケジュール情報取得
	var getEvents = function() {
		var $schedules = $("div.showtime div.critical");
		
		var events = [];
		$schedules.each(function(index) {
			var event = new Event();
			event.title = $(this).find("a").text();
			var link = baseUrl + $(this).find("a").attr("href");
			event.eventId = extractEventId(link);
			event.url = "https://caga.cyberagent.co.jp/cgi-bin/cbgrn/grn.cgi/schedule/view?event=" + event.eventId;
			events[index] = event;
		});
	};
	//URLからeventId抽出
	var extractEventId = function(url) {
		var fromIndex = url.indexOf("event=");
		if(fromIndex == -1) return "";
		var toIndex = url.indexOf("&", fromIndex + 1);
		var eventId = url.substring(fromIndex + 4, toIndex);
		return eventId;
	}
	//スケジュール設定
	var setCalender = function() {
		// $("#view").empty();
		$(".critical").css("overflow", "hidden");
		
		$("#view").css("width", "70%").css("margin", "0 auto");

		var date = new Date();
		var d = date.getDate();
		var m = date.getMonth();
		var y = date.getFullYear();

		$("#view").fullCalendar({
			header : {
				left : 'prev,next',
				center : 'title',
				right : 'today,agendaDay,agendaWeek,month'
			},
			editable : true,
			events : [
						allDay('All Day Event', new Date(y, m, 1)), 
						longEvent("Long Event Object", new Date(y, m, d - 5), 
						new Date(y, m, d - 2)), repeat(999, 'Repeating Event1', 
						new Date(y, m, d - 3, 16, 0)), repeat(999, 'Repeating Event2', 
						new Date(y, m, d + 4, 16, 0)), oneTime('ミーティング', 
						new Date(y, m, d, 10, 30), new Date(y, m, d, 11, 30)), 
						oneTime('Meeting', new Date(y, m, d, 11, 00), new Date(y, m, d, 12, 30)), 
						oneTime('ランチ', new Date(y, m, d, 12, 00), new Date(y, m, d, 13, 30)), 
						oneTime('懇親会', new Date(y, m, d, 19, 00), new Date(y, m, d, 22, 00)), 
						longEvent('Has Link', new Date(y, m, 28), new Date(y, m, 29), 'http://google.com/')
					]
		});
	};
	//Event class
	var Event = function() {
		this.allDay = false;
	}
	function longEvent(title, start, end, url) {
		var event = new Event();
		event.title = title;
		event.start = start;
		event.end = end;
		event.url = url;
		event.allDay = true;
		return event;
	}

	function allDay(title, date, url) {
		var event = new Event();
		event.title = title;
		event.start = date;
		event.url = url;
		event.allDay = true;
		return event;
	}

	function repeat(id, title, start, url) {
		var event = new Event();
		event.id = id;
		event.title = title;
		event.start = start;
		event.url = url;
		return event;
	}

	function oneTime(title, start, end, url) {
		var event = new Event();
		event.title = title;
		event.start = start;
		event.end = end;
		event.url = url;
		return event;
	}

	//-------------------------------
	//処理
	//-------------------------------
	var events = getEvents();
	setCalender();
});
